﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;
using Beetle.Sl.EditorControl.Core;

namespace Beetle.Sl.EditorControl.Controls
{
    public partial class DocImage : UserControl, IEditorControl
    {
        private Point? startPoint = null;
        private Point? endPoint = null;
        private Point? currentPoint = null;

        private Editor EditorInst
        {
            get;
            set;
        }

        private byte[] imageBytes = null;

        public DocImage()
        {
            InitializeComponent();
            this.dragEllipse.MouseLeftButtonDown += new MouseButtonEventHandler(dragEllipse_MouseLeftButtonDown);
            this.dragEllipse.MouseLeftButtonUp += new MouseButtonEventHandler(dragEllipse_MouseLeftButtonUp);
        }

        void dragEllipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.dragEllipse.CaptureMouse();
            this.frameEllipse.Width = this.ActualWidth;
            this.frameEllipse.Height = this.ActualHeight;
            //  this.dragEllipse.Visibility = System.Windows.Visibility.Collapsed;
            this.frameEllipse.Visibility = System.Windows.Visibility.Visible;
            this.popup.IsOpen = true;
            this.startPoint = e.GetPosition(this);
            this.MouseMove += new MouseEventHandler(DocImage_MouseMove);
        }

        void DocImage_MouseMove(object sender, MouseEventArgs e)
        {
            this.currentPoint = e.GetPosition(this);
            this.frameEllipse.Width = Math.Abs(this.ActualWidth + this.currentPoint.Value.X - this.startPoint.Value.X);
            this.frameEllipse.Height = Math.Abs(this.ActualHeight + this.currentPoint.Value.Y - this.startPoint.Value.Y);
        }

        void dragEllipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.MouseMove -= new MouseEventHandler(DocImage_MouseMove);
            this.dragEllipse.ReleaseMouseCapture();
            this.endPoint = e.GetPosition(this);

            //  this.dragEllipse.Visibility = System.Windows.Visibility.Visible;
            this.frameEllipse.Visibility = System.Windows.Visibility.Collapsed;
            this.popup.IsOpen = false;

            if (this.startPoint != null && this.endPoint != null)
            {
                this.Width = Math.Abs(this.ActualWidth + this.endPoint.Value.X - this.startPoint.Value.X);
                this.Height = Math.Abs(this.ActualHeight + this.endPoint.Value.Y - this.startPoint.Value.Y);

                if (this.mainImage.Stretch == Stretch.None)
                {
                    this.mainImage.Stretch = Stretch.Fill;
                }
            }

            this.startPoint = null;
            this.endPoint = null;
        }

        public void ResourceLoaded(Editor editorInst)
        {
            this.EditorInst = editorInst;
            this.EditorInst.EditorStatusChanged += new EventHandler(EditorInst_EditorStatusChanged);

            if (!double.NaN.Equals(this.Width) && !double.NaN.Equals(this.Height))
            {
                this.mainImage.Stretch = Stretch.Fill;
            }
        }

        void EditorInst_EditorStatusChanged(object sender, EventArgs e)
        {
            switch (this.EditorInst.EditorStatus)
            {
                case EditorStatusEnum.Edit:
                    this.dragEllipse.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                case EditorStatusEnum.Designer:
                    this.dragEllipse.Visibility = System.Windows.Visibility.Visible;
                    break;
                case EditorStatusEnum.View:
                    this.dragEllipse.Visibility = System.Windows.Visibility.Collapsed;
                    break;
                default:
                    break;
            }
        }

        public bool IsHasResours()
        {
            return true;
        }

        public string ResId { get; set; }

        public byte[] GetBytes()
        {
            return this.imageBytes;
        }

        public void SetBytes(string resId, byte[] bytes)
        {
            this.ResId = resId;
            this.imageBytes = bytes;
            this.ShowImage(bytes);
        }

        private void ShowImage(byte[] bytes)
        {
            if (bytes != null && bytes.Length != 0)
            {
                MemoryStream stream = new MemoryStream();
                stream.Write(bytes, 0, bytes.Length);

                BitmapImage bi = new BitmapImage();
                bi.SetSource(stream);
                this.mainImage.Source = bi;
            }
            else
            {
                this.mainImage.Source = null;
            }
        }

        public string ResString
        {
            get;
            set;
        }

        public void Serialize()
        {
            if (this.imageBytes != null)
            {
                this.ResString = Convert.ToBase64String(this.imageBytes);
            }
            else
            {
                this.ResString = string.Empty;
            }
        }


        public void SetXaml(string xaml)
        {
            this.SetBytes(Guid.NewGuid().ToString(), Convert.FromBase64String(xaml));
        }
    }
}
